package yxy.game.pm2.command.tower;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TowerMessage;
import yxy.game.pm2.module.battle.FighterModule;
import yxy.game.pm2.module.tower.TowerHandle;

import java.util.ArrayList;

/**
 * 修炼塔 层信息
 */
@MessageType(proto = Command.TowerFloor, classtype = TowerMessage.TowerFloorRequest.class)
public class CMD_TowerFloor extends Command<TowerMessage.TowerFloorRequest> {

    @Override
    public void execute(MessageSource messageSource, TowerMessage.TowerFloorRequest request) {

        String playerCode = messageSource.getTag();

        int floor = request.getFloor();

        TowerMessage.TowerFloorResponse.Builder response = TowerMessage.TowerFloorResponse.newBuilder();

        TowerHandle towerHandle = new TowerHandle();

        towerHandle.packTowerFloor(playerCode, floor, response);

        getTotalPower(towerHandle,floor,response);

        send(messageSource, response, Result.Success);

    }

    private void send(MessageSource messageSource, TowerMessage.TowerFloorResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


    //获取总战力
    public void getTotalPower(TowerHandle towerHandle , int floor , TowerMessage.TowerFloorResponse.Builder response){
        ArrayList<FighterModule> fighterModuleList = towerHandle.getFighterModuleList(floor);
        long totalPower = 0;
        for (FighterModule fighterModule : fighterModuleList) {
            long power = fighterModule.getTotalPower();
            totalPower += power;
        }
        response.setTotalPower(totalPower);
    }
}